package org.moore.controller;

import org.moore.model.Map;
import org.moore.model.Score;
import org.moore.model.User;
import org.moore.utils.Countdown;
import org.moore.utils.Data;
import org.moore.utils.MusicUtils;
import org.moore.utils.ScoreUtils;
import org.moore.view.*;

import javax.swing.*;

// 程序运行类
public class Application {
    // 创建欢迎面板对象
    private static final WelcomePanel welcomePanel = new WelcomePanel();
    // 创建欢迎窗口对象
    private static final WelcomeJFrame welcomeJFrame = new WelcomeJFrame(welcomePanel);
    // 创建游戏面板对象
    private static final GamePanel gamePanel = new GamePanel();
    // 创建背景音乐对象
    private static final MusicUtils musicBackground = new MusicUtils("Music/bgmusic.wav");
    // 创建胜利音乐对象
    private static final MusicUtils musicWin = new MusicUtils("Music/win.wav");
    // 创建失败音乐对象
    private static final MusicUtils musicLoss = new MusicUtils("Music/loss.wav");
    // 创建游戏窗口对象
    private static final GameFrame gameFrame = new GameFrame(gamePanel);
    // 创建存档面板对象
    private static final SaveMapPanel saveMapPanel = new SaveMapPanel();
    // 创建存档窗口对象
    private static final SaveMapFrame saveMapFrame = new SaveMapFrame(saveMapPanel);
    // 定义计数用于第几轮游戏
    private static int count = 0;
    // 定义过关分数
    private static int MaxScore = 300;
    // 获取过关分数
    public static int getMaxScore() {
        return MaxScore;
    }
    // 程序运行的方法
    public static void run() {
        // 默认播放音乐
        musicBackground.setLoop(true);
        musicBackground.play();
        // 展示面板
        welcomeJFrame.setVisible(true);
        // 由于有开屏动画，需要睡眠一段时间再运行添加监听
        try {
            Thread.sleep(6500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 创建欢迎界面登录按钮监听
        LoginButtonListener loginButtonListener = new LoginButtonListener(welcomePanel);
        // 安装欢迎界面登录按钮监听
        WelcomePanel.getWelcomeJLabel().getLoginButton().addMouseListener(loginButtonListener);
        // 创建登录界面确认登录按钮监听
        YesLoginButtonListener yesLoginButtonListener = new YesLoginButtonListener(welcomePanel,gameFrame,welcomeJFrame);
        // 安装登录界面确认登录按钮监听
        WelcomePanel.getLoginJLabel().getYesButton().addMouseListener(yesLoginButtonListener);
        // 创建登录验证码监听
        RandomNumListener randomNumListener = new RandomNumListener();
        // 安装登录验证码监听
        WelcomePanel.getLoginJLabel().getCheckNum().addMouseListener(randomNumListener);
        // 创建欢迎界面注册界面监听
        RegisterButtonListener registerButtonListener = new RegisterButtonListener(welcomePanel);
        // 安装欢迎界面注册界面监听
        WelcomePanel.getWelcomeJLabel().getRegisterButton().addMouseListener(registerButtonListener);
        // 创建欢迎界面游客跳转监听
        TouristButtonListener touristButtonListener = new TouristButtonListener(gameFrame,welcomeJFrame);
        // 安装创建欢迎界面游客跳转监听
        WelcomePanel.getWelcomeJLabel().getTouristButton().addMouseListener(touristButtonListener);
        // 创建返回按钮监听
        BackButtonListener backButtonListener = new BackButtonListener(welcomePanel);
        // 安装登录界面返回按钮监听
        WelcomePanel.getLoginJLabel().getBackButton().addMouseListener(backButtonListener);
        // 安装注册界面返回按钮监听
        WelcomePanel.getRegisterJLabel().getBackButton().addMouseListener(backButtonListener);
        // 创建注册界面确认注册按钮监听
        YesRegisterButtonListener yesRegisterButtonListener = new YesRegisterButtonListener(welcomePanel);
        // 安装注册界面确认注册按钮监听
        WelcomePanel.getRegisterJLabel().getYesButton().addMouseListener(yesRegisterButtonListener);
        // 创建提示按钮监听
        TipButtonListener tipButtonListener = new TipButtonListener();
        // 游戏界面提示按钮安装监听
        GamePanel.getGameLabel().getTip().addMouseListener(tipButtonListener);
        // 创建暂停按钮监听
        PauseButtonListener pauseButtonListener = new PauseButtonListener();
        // 安装暂停界面监听
        GamePanel.getGameLabel().getPause().addMouseListener(pauseButtonListener);
        // 创建游戏模式监听
        GameModeButtonListener gameModeButtonListener = new GameModeButtonListener(gamePanel);
        // 安装游戏模式监听
        GamePanel.getHomeLabel().getTimeMode().addActionListener(gameModeButtonListener);
        GamePanel.getHomeLabel().getStepMode().addActionListener(gameModeButtonListener);
        GamePanel.getHomeLabel().getDescription().addActionListener(gameModeButtonListener);
        GamePanel.getHomeLabel().getExitGame().addActionListener(gameModeButtonListener);
        // 创建关卡选择监听
        LevelButtonListener levelButtonListener = new LevelButtonListener(gamePanel);
        // 安装关卡选择监听
        GamePanel.getLevelLabel().getLowerButton().addActionListener(levelButtonListener);
        GamePanel.getLevelLabel().getMiddenButton().addActionListener(levelButtonListener);
        GamePanel.getLevelLabel().getPremiumButton().addActionListener(levelButtonListener);
        GamePanel.getLevelLabel().getBackButton().addActionListener(levelButtonListener);
        // 创建菜单栏监听
        GameMenuListener gameMenuListener =new GameMenuListener(gameFrame,gamePanel,saveMapFrame,saveMapPanel, musicBackground);
        // 安装菜单栏监听
        GameFrame.getGameBar().getStartItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getRestartItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getExitItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getRoadItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getRankingListItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getHomeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getBlockColorItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getMapShapeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getObstaclesNumberItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getObstaclesDisappearItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getObstaclesAppearItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getMusicOffItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getMusicOnItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getGameModeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getStepsNumberItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getTimeNumberItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getLowerModeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getMiddenModeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getPremiumModeItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getIntroItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getAboutItem().addActionListener(gameMenuListener);
        GameFrame.getGameBar().getDonationItem().addActionListener(gameMenuListener);
        // 添加存档监听
        SaveMapListener saveMapListener = new SaveMapListener(saveMapPanel,saveMapFrame);
        // 安装存档监听
        saveMapPanel.getSaveMapBtn().addActionListener(saveMapListener);
        saveMapPanel.getSave1().addActionListener(saveMapListener);
        saveMapPanel.getSave2().addActionListener(saveMapListener);
        saveMapPanel.getSave3().addActionListener(saveMapListener);
        saveMapPanel.getSave4().addActionListener(saveMapListener);
        saveMapPanel.getSave5().addActionListener(saveMapListener);
        saveMapPanel.getSave6().addActionListener(saveMapListener);
        // 添加欢迎窗口监听
        WelcomeWindowListener welcomeWindowListener = new WelcomeWindowListener();
        // 安装欢迎窗口监听
        welcomeJFrame.addWindowListener(welcomeWindowListener);
        // 添加游戏窗口监听
        GameWindowListener gameWindowListener = new GameWindowListener(gameFrame,welcomeJFrame,gamePanel);
        // 安装游戏窗口监听
        gameFrame.addWindowListener(gameWindowListener);
    }

    // 重来下一关与提示的方法（实在不知道加在哪个类好，只能写在这里进行调用惹）
    public static void nextLevel() {
        // 每过一轮计数加1
        count++;
        // 更改过关分数
        MaxScore = 300 * count;
        // 判断分数是否达标
        if (Data.SCORE >= MaxScore) {
            // 播放胜利音乐
            musicWin.play();
            // 过关可选择是否继续、返回主菜单或者下一关
            String[] options = {"返回主界面", "继续游戏", "下一关"};
            int input = JOptionPane.showOptionDialog(null, "您当前得分" + Data.SCORE + "\n恭喜您通关本关卡\n请选择您的选项：", "游戏过关",
                    JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                    null, options, options[0]);
            // 返回主界面
            if (input == 0) {
                GamePanel.getCardLayout().show(gamePanel,"home");
                // 继续游戏
            } else if (input == 1) {
                JOptionPane.showConfirmDialog(null,"继续游戏，将重新开始计数");
                // 重新开始计时或者重新计步
                if (Map.isMapType() == 1) {
                    Data.STEPS = 15;
                } else if (Map.isMapType() == 2) {
                    try {
                        Countdown.pause();
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    new Countdown(40);
                    Data.COUNT = 0;
                }
                // 下一关
            } else if (input == 2 ) {
                if (!Data.TOURIST) {
                    // 分数是否保存至排行榜
                    Object[] listOptions = { "保存", "取消"};
                    int listInput = JOptionPane.showOptionDialog(gameFrame,"要将上一局的分数保存至排行榜吗？", "保存分数",
                            JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,null,listOptions,listOptions[0]);
                    if (listInput == 0) {
                        ScoreUtils.insertScore(new Score(666, User.getId(), Map.isMapType(), Map.getLevel(), Data.SCORE));
                    }
                }
                // 只有三关，所以到第三关选下一关也是返回到主菜单
                if (Map.getLevel() == 3) {
                    JOptionPane.showConfirmDialog(null,"没有下一关了，将返回主界面！");
                    GamePanel.getCardLayout().show(gamePanel,"home");
                    // 否则（也就是一二关）进入下一关
                } else {
                    JOptionPane.showConfirmDialog(null,"即将进入下一关");
                    new Map(9,9,Map.getLevel() +1);
                    // 重置计数
                    if (Map.isMapType() == 1) {
                        Data.STEPS = 15;
                    } else if (Map.isMapType() == 2) {
                        try {
                            Countdown.pause();
                        } catch (Exception ex) {
                            System.out.println();
                        }
                        Data.COUNT = 0;
                        new Countdown(40);
                    }
                    Data.CUSTOM = 0;
                    Data.SCORE = 0;
                    count = 0;
                }
            }
            // 否则（也就是未达到300分）只能重新开始，或者返回主界面
        } else {
            // 播放失败音乐
            musicLoss.play();
            // 失败选项
            Object[] restartOptions = { "返回主界面", "重新开始" };
            int restartInput = JOptionPane.showOptionDialog(gameFrame, "很遗憾，您未达指定分数，请重试", "游戏失败",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, restartOptions, restartOptions[0]);
            // 返回主界面
            if (restartInput == 0) {
                GamePanel.getCardLayout().show(gamePanel,"home");
                // 重新开始
            } else if (restartInput == 1) {
                new Map(9,9,Map.getLevel());
                // 重置数值
                Data.SCORE = 0;
                if (Map.isMapType() == 1) {
                    Data.STEPS = 15;
                } else if (Map.isMapType() == 2) {
                    try {
                        Countdown.pause();
                    } catch (Exception ignored) {
                        System.out.println();
                    }
                    new Countdown(40);
                    Data.COUNT = 0;
                }
                Data.CUSTOM = 0;
                count = 0;
            }
        }
    }
}
